
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

int main(int argc, char **argv)
{

    if (argc < 2)
    {
        printf("Usage: %s [normal|recursive|error]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    pthread_mutex_t mutex;

    pthread_mutexattr_t mutex_attr;
    pthread_mutexattr_init(&mutex_attr);

    if ((strncmp(argv[1], "normal", strlen("normal"))) == 0)
    {
        pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_NORMAL);
    }
    else if ((strncmp(argv[1], "recursive", strlen("recursive"))) == 0)
    {
        pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
    }
    else if ((strncmp(argv[1], "error", strlen("error"))) == 0)
    {
        pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
    }
    else
    {
        printf("Mutex Attr Error!\n");
        exit(EXIT_FAILURE);
    }

    pthread_mutex_init(&mutex, &mutex_attr);

    if (pthread_mutex_lock(&mutex) == 0)
    {
        printf("Pthread Lock Success!\n");
    }
    else
    {
        printf("Pthread Lock Failure!\n");
    }
    sleep(1);
    if (pthread_mutex_lock(&mutex) == 0)
    {
        printf("Pthread Lock Success!\n");
    }
    else
    {
        printf("Pthread Lock Failure!\n");
    }
    sleep(1);
    if (pthread_mutex_lock(&mutex) == 0)
    {
        printf("Pthread Lock Success!\n");
    }
    else
    {
        printf("Pthread Lock Failure!\n");
    }

    pthread_mutex_unlock(&mutex);
    pthread_mutex_unlock(&mutex);

    pthread_mutexattr_destroy(&mutex_attr);
    pthread_mutex_destroy(&mutex);

    return 0;
}